const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-')
}
const formatSecond = timer => {
  var time = "00:00"
  if(timer){
    var s = timer % 60
    var m = parseInt(timer / 60)   
    time = (m > 9 ? m : '0' + m) + ":"+(s > 9 ? s : '0' + s) 
  }
  return time
}
const getQueryString = (name,path)=>{
  try {
    let search = path.split("?")[1]
    var  reg =  new  RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var  r = search.match(reg); 
    if(r!=null)  return  unescape(r[2]);  return  null;
  } catch (error) {
    return  null;
  }
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatSecond: formatSecond,
  getQueryString: getQueryString
}
